import requests
import app


class MiniProgram(object):
    def __init__(self):
        self.banner_url = app.base_url + '/' + 'banner/1'
        self.theme_url = app.base_url + '/' + 'theme'
        self.recent_url = app.base_url + '/' + 'product/recent'
        self.all_url = app.base_url + '/' + 'category/all'
        self.category_url = app.base_url + '/' + 'product/by_category'
        self.product_url = app.base_url + '/' + 'product/'
        self.order_url = app.base_url + '/' + 'order/by_user'
        self.created_url = app.base_url + '/' + 'order'
        self.search_url = app.base_url + '/' + 'order' + '/'
        self.token_url = app.base_url + '/' + 'token/user'
        self.verify_url = app.base_url + '/' + 'token/verify'
        self.address_url = app.base_url + '/' + 'address'

    def banner(self):
        # 轮播图接口
        return requests.get(url=self.banner_url)

    def theme(self, number):
        # 主题商品 ,可以测试不同数目的专题栏位显示对应接口测试用例
        # number = {'ids': ids}
        return requests.get(url=self.theme_url, params=number)

    def new_product(self):
        # 最近新品
        return requests.get(url=self.recent_url)

    def category_all(self):
        # 商品分类
        return requests.get(url=self.all_url)

    def by_category(self, num):
        # 商品分类下的商品
        return requests.get(url=self.category_url, params={'id': num})

    def product(self, num):
        # 查看商品信息
        return requests.get(url=self.product_url + num)

    def user_order(self, num, token):
        # 查看订单
        return requests.get(url=self.order_url, params={'page': num}, headers=token)

    def new_order(self, data):
        # 创建订单
        return requests.post(url=self.created_url, headers=app.headers, json=data)

    def see_order(self, num, token):
        return requests.get(url=self.search_url + num, headers=token)

    def get_token(self, data):
        # 获取token
        return requests.post(url=self.token_url, headers=app.headers1, json=data)

    def check_token(self, data):
        # 验证token
        return requests.post(url=self.verify_url, headers=app.headers1, json=data)

    def address(self, token):
        # 查看地址信息
        return requests.get(url=self.address_url, headers=token)


if __name__ == '__main__':
    mini = MiniProgram()
    response = mini.banner()  # 轮播图
    print(response.json())
    print(response.status_code)
    response1 = mini.theme({'ids': '1,2,3'})  # 主题商品
    print(response1.json())
    response2 = mini.new_product()  # 最近新品
    print(response2.json())
    response3 = mini.category_all()  # 商品分类
    print(response3.json())
    response4 = mini.by_category(2)  # 分类下商品
    print(response4.json())
    response5 = mini.product('2')  # s商品信息
    print(response5.json())
    token = {'token': '32e7535b8c2133af216dd7d7bc5517f6'}
    response6 = mini.user_order(1, token)  # 查看订单列表
    print(response6.json())
    app.headers['token'] = '32e7535b8c2133af216dd7d7bc5517f6'
    data = {"products": [{"product_id": 8, "count": 1}, {"product_id": 10, "count": 2}]}
    response7 = mini.new_order(data)
    print(response7.json())
    response8 = mini.see_order('108', token)
    print(response8.json())
    # data = {'code': '083r5f0w3qIWRU2InV0w3pSLFY1r5f0B'}
    # # 不能同时调用app.headers已经改变
    # response9 = mini.get_token(data)
    # print(response9.json())
    data = {'token': '32e7535b8c2133af216dd7d7bc5517f6'}
    app.headers1['Content-Type'] = 'application/json'
    response10 = mini.check_token(data)
    print(response10.json())
    response11 = mini.address(data)
    print(response11.json())
    app.headers.pop('token')
    print(app.headers)